package com.mgl.movesprite.game.model;

import java.util.LinkedList;
import java.util.Queue;

import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.mgl.movesprite.EntitySpriteLevelBase;
import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.movesprite.game.factory.GameTextureManager;
import com.mgl.movesprite.game.factory.GameType;
import com.mgl.util.Path;

public class BeginPoint extends EntitySpriteLevelBase {

	private boolean sendWave = true;
	private Queue<Wave> waveList;
	private Path path;

	private float cont = 0;
	private float timeToSendWave = 10;
	
	private GameObjectFactory gameObjectFactory = null;

	public BeginPoint(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,
			GameObjectFactory gameObjectFactory) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager,
				gameObjectFactory);

		this.gameObjectFactory = gameObjectFactory;
		
		

	}

	public void init(Path path){
		
		this.path =  path.clone();
		initWaveList();
		
	}
	
	private void initWaveList() {

		waveList = new LinkedList<Wave>();

		for (int i = 0; i < 10; i++) {

			Wave wave = (Wave) gameObjectFactory.createObject(GameType.WAVE,0,0);
			wave.init(path);
			wave.setVisible(false);
			waveList.add(wave);

		}

	}

	public void update(float pSecondsElapsed, Level level) {
		try {
			if (!sendWave || waveList.isEmpty()) {
				return;
			}

			cont += pSecondsElapsed;
			
			if (cont > timeToSendWave) {
				
				cont = 0;
				Wave wave = waveList.poll();			
				level.addEntity(wave);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
